#ifndef VIDEO_H_
# define VIDEO_H_

# ifndef _POSIX_C_SOURCE
#  define _POSIX_C_SOURCE 199309L
# endif

# include <stdio.h>
# include <time.h>
# include <string.h>
# include <ncurses.h>
# include <pthread.h>
# include <libavcodec/avcodec.h>
# include <libavformat/avformat.h>
# include <libswscale/swscale.h>
/* # include <caml/threads.h> */
# include "caml.h"
# include "thqueue.h"
# include "sound_output.h"

# define AV_SYNC_VIDEO_MASTER -1 /* grepme */
# define AV_SYNC_AUDIO_MASTER -1 /* grepme */

# define BUFSIZE	AVCODEC_MAX_AUDIO_FRAME_SIZE
# define DEFAULT_AV_SYNC_TYPE	AV_SYNC_VIDEO_MASTER

typedef			struct _video
{
  AVFormatContext	*pFormatCtx;

  AVCodecContext	*pCtx;
  AVCodecContext	*pAudioCodec;

  AVCodec		*pCodec;
  AVCodec		*pAudio;

  AVFrame		*pRaw;
  AVFrame		*pDat;
  uint8_t		*buffer;

  pa_simple		*pass;

  struct SwsContext	*Sctx;

  int			videoStream;
  int			width;
  int			height;
  int			format;
  int			fluxAudio;

  pthread_t		*ath;
  pthread_t		*vth;

  pktqueue              audio_queue;
  pktqueue              video_queue;

  struct timespec	*tim;

  int			new_frame; /* boolean */
  int			seek_req;
  int			seek_flags;
  int64_t		seek_pos;
  int64_t		video_current_pts_time;
  int			av_sync_type;

  double		audio_clock;
  unsigned int		audio_buf_index;
  unsigned int		audio_buf_size;
  double		video_current_pts;

} video;

value			pixel(video				*cur,
			      value				i,
			      value				j);

value			width(video				*current);

value			height(video				*current);

void			*audio_th(void				*current);

void			*video_th(void				*current);

video			*video_init(value			file,
				    value			ba);
value			video_next(video			*cur);
value			video_quit(video			*cur);

value			video_debug_ppm(value			cur,
					value			file);

void			stream_seek(video			*is,
				    int64_t			pos,
				    int				rel);

double			get_audio_clock(video			*is);
double			get_video_clock(video			*is);
double			get_external_clock(/* VideoState */video		*is);
double			get_master_clock(video			*is);


#endif /* !VIDEO_H_ */
